package com.sinodoggo.shitcloud.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinodoggo.shitcloud.commons.Result;
import com.sinodoggo.shitcloud.entity.Menu;
import com.sinodoggo.shitcloud.service.IMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sinodoggo
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private IMenuService menuService;

    /**
     * 以树形结构返回菜单
     * @return 结果集
     */
    @GetMapping
    public Result getAllMenus(@RequestParam(value = "name", defaultValue = "") String name) {
        List<Menu> parentMenu = menuService.getAllMenus(name);
        return Result.success(parentMenu);
    }

    @DeleteMapping("/{id}")
    public Result deleteMenuById(@PathVariable Integer id) {
        menuService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatchByIds(@RequestBody List<Integer> ids) {
        menuService.removeBatchByIds(ids);
        return Result.success();
    }

    @GetMapping("/info/{id}")
    public Result getSpecificMenuById(@PathVariable Integer id) {
        final Menu menu = menuService.getById(id);
        return Result.success(menu);
    }

    @PostMapping
    public Result saveOrUpdateMenu(@RequestBody Menu menu) {
        menuService.saveOrUpdate(menu);
        return Result.success();
    }
}

